package com.cn.wanxi.service;

import com.cn.wanxi.common.Message;
import com.cn.wanxi.dao.IExam;
import com.cn.wanxi.entity.ExamEntity;
import com.cn.wanxi.entity.ExamPaperEntity;
import com.cn.wanxi.entity.GradeInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author:
 * time: 2020/3/7
 * describe:
 */
@Service
public class ExamService {

    @Autowired
    private IExam exam;

    /**
     * 通过试卷id进入考试并查询试题
     *
     * @param examPaperId 试题id
     * @return 返回试题
     */
    public List<ExamEntity> ExamPaper(Integer examPaperId) {
        return exam.getExamPaperById(examPaperId);
    }

    /**
     * 获取考试答案，并传回最终分数
     *
     * @param studentEntities
     * @return 考试分数
     */
    public Integer getMark(List<ExamEntity> studentEntities, Integer examId) {
        Integer countChoice = 0; //选择题总数量
        Integer trueChoice = 0; //选正确的数量
        Integer countGapFill = 0;
        Integer trueGapFill = 0;
        Integer countTF = 0;
        Integer trueTF = 0;
        Integer choiceMark = 0; //选择题总分
        Integer gapFillMark = 0;
        Integer TFMark = 0;
        List<ExamEntity> answerList = exam.getExamPaperAnswer(examId); //获取答案
        Next:
        for (int i = 0; i < answerList.size(); i++) {
            for (int s = 0; s < studentEntities.size(); s++) {
                if (answerList.get(i).getExamId() == studentEntities.get(s).getExamId()) {
                    switch (answerList.get(i).getExamType()) {
                        case "单选题":
                            if (answerList.get(i).getExamAnswer().trim().equals(studentEntities.get(s).getExamAnswer().trim())) {
                                countChoice++;
                                trueChoice++;
                                continue Next;
                            } else {
                                countChoice++;
                                continue Next;
                            }
                        case "多选题":
                            if (isEquals(answerList.get(i).getExamAnswer(), studentEntities.get(s).getExamAnswer())) {
                                countChoice++;
                                trueChoice++;
                                continue Next;
                            } else {
                                countChoice++;
                                continue Next;
                            }
                        case "判断题":
                            if (answerList.get(i).getExamAnswer().equals(studentEntities.get(s).getExamAnswer())) {
                                countTF++;
                                trueTF++;
                                continue Next;
                            } else {
                                countTF++;
                                continue Next;
                            }
                        case "填空题":
                            if (answerList.get(i).getExamAnswer().equals(studentEntities.get(s).getExamAnswer())) {
                                countGapFill++;
                                trueGapFill++;
                                continue Next;
                            } else {
                                countGapFill++;
                                continue Next;
                            }
                        default:
                            continue Next;
                    }
                }
            }
        }
        if (countChoice != 0) {
            if (countChoice == trueChoice) {
                choiceMark = answerList.get(0).getSelectMark();
            } else {
                choiceMark = answerList.get(0).getSelectMark() / countChoice * trueChoice;
            }
        } else {
            choiceMark = answerList.get(0).getSelectMark();
        }
        if (countGapFill != 0) {
            if (countGapFill == trueGapFill) {
                gapFillMark = answerList.get(0).getGapFillMark();
            } else {
                gapFillMark = answerList.get(0).getGapFillMark() / countGapFill * trueGapFill;
            }
        } else {
            gapFillMark = answerList.get(0).getGapFillMark();
        }
        if (countTF != 0) {
            if (countTF == trueTF) {
                TFMark = answerList.get(0).getTFMark();
            } else {
                TFMark = answerList.get(0).getTFMark() / countTF * trueTF;
            }
        } else {
            TFMark = answerList.get(0).getTFMark();
        }
        return choiceMark + gapFillMark + TFMark;
    }

    /**
     * 验证多选在顺序不同的情况下是否相等：注意大小写必须一样，不能有空格
     *
     * @param s
     * @param t
     * @return
     */
    private boolean isEquals(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        int[] counter = new int[256];
        for (char c : s.toCharArray()) {
            counter[c]++;
        }
        // 两字符串长度一致的前提下
        for (char c : t.toCharArray()) {
            if (--counter[c] < 0) {
                return false;
            }
        }
        return true;
    }


    /**
     * 插入学生的分数
     *
     * @param entity
     * @return 插入是否成功
     */
    public Message AddMark(GradeInfoEntity entity) {
        Map<String, Object> map = new HashMap<>();
        map.put("考试成绩", entity.getGradeExam());
        if (exam.AddStudentMark(entity) > 0) {
            return new Message(1, "", map);
        }
        return new Message(0, "添加成绩错误", map);
    }


    /**
     * 获取一个学生所有需要参加的考试
     *
     * @param student_Id 学生id
     * @return
     */
    public List<ExamPaperEntity> getAllExam(Integer student_Id) {
        return exam.getAllExam(student_Id);
    }

    public List<ExamPaperEntity> getPaper(String subject) {
        return exam.getPaper(subject);
    }

    public Message CompleteExamInformation(ExamPaperEntity entity) {
        Integer result = exam.CompleteExamInformation(entity);
        if (result > 0) {
            return new Message(1, "", result);
        }
        return new Message(0, "", null);
    }

    public Message DistributeExam(Integer examId, Integer studentGrade) {
        Integer res = 0;
        res = exam.DistributeExam(examId, studentGrade);
        if (res > 0) {
            return new Message(1, "执行结果", res);
        }
        return new Message(0, "", null);
    }
}
